package com.cafuc.entity;

import com.cafuc.base.JavaBase;

import java.io.*;
import java.util.Arrays;
import java.util.Date;

@MyAnnotation(type = 1, value = "wangfangqi",
        imagetype = ImageType.SMALL_IMAGE, cities = {"cd", "cq"})
public class User implements Serializable, Cloneable {
    private int id;
    @MyAnnotation(type = 2, value = "wangfangqi2",
            imagetype = ImageType.SMALL_IMAGE, cities = {"cd2", "cq2"})
    private String name;
    private int age;
    private  Card card;
    private String password;
    private Date createDate;

    public User() {
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    @MyAnnotation(type = 3, value = "wangfangqi3",
            imagetype = ImageType.SMALL_IMAGE, cities = {"cd3", "cq3"})
    public void call(){

        System.out.println("++");
    }

    public static void display(){
        System.out.println("dfsfaf");
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public User deepClone() throws IOException, ClassNotFoundException {
        //使用 ObjectOutputStream 将内存的当前对象序列化到 ByteArrayOutputStream
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(this);

        //使用 ObjectInputStream 将字节数组中的内容反序列化为对象输出
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        //User user = ois.readObject();
        return (User) ois.readObject();
    }
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException, IOException, ClassNotFoundException {
        JavaBase javaBase = new JavaBase(){
            @Override
            public void call() {
                System.out.println("hello");
            }
        };
        javaBase.call();
        //在类下的自定义注解
        MyAnnotation myAnnotation1 = User.class.getAnnotation(MyAnnotation.class);
        System.out.println(myAnnotation1.type() + "==" + myAnnotation1.value() + "--"
                + Arrays.asList(myAnnotation1.cities()));
        //在属性上的自定义注解
        MyAnnotation myAnnotation2 = User.class.getDeclaredField("name").getAnnotation(MyAnnotation.class);
        System.out.println(myAnnotation2.type() + "==" + myAnnotation2.value() + "--"
                + Arrays.asList(myAnnotation2.cities()));
        //在方法下的自定义注解
        MyAnnotation myAnnotation3 = User.class.getDeclaredMethod("call", null).getAnnotation(MyAnnotation.class);
        System.out.println(myAnnotation3.type() + "==" + myAnnotation3.value() + "--"
                + Arrays.asList(myAnnotation3.cities()));


        Card card = new Card();
        card.setId(1);
        card.setNumber("6127817668791");
        User source = new User();
        source.setId(1);
        source.setName("daxunhua");
        source.setAge(18);
        source.setCard(card);

        //User target = (User) source.clone();
        User target = (User) source.deepClone();
        source.setAge(19);
        source.setName("huaer");
        source.getCard().setNumber("213819893");
        System.out.println(source.getName() + "-" + source.getAge() + "-" +
                source.getCard().getNumber());
        System.out.println(target.getName() + "-" + target.getAge() + "-" +
                target.getCard().getNumber());

    }
}
